﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace GalaxyDefenderChaos
{
    public abstract class Camera : InvisibleGameEntity
    {
        protected Matrix view;
        protected Matrix projection;

        protected GraphicsDevice GraphicsDevice { get; set; }
        public BoundingFrustum Frustum { get; private set; }
        public Matrix Projection
        {
            get { return projection; }
            protected set
            {
                projection = value;
                generateFrustum();
            }
        }
        public Matrix View
        {
            get { return view; }
            protected set
            {
                view = value;
                generateFrustum();
            }
        }
        private void generateFrustum()
        {
            Matrix viewProjection = View * Projection;
            Frustum = new BoundingFrustum(viewProjection);
        }

        public Camera(GraphicsDevice graphicsDevice)
        {
            this.GraphicsDevice = graphicsDevice;
            generatePerspectiveProjectionMatrix(MathHelper.PiOver4);
        }
        private void generatePerspectiveProjectionMatrix(float FieldOfView)
        {
            PresentationParameters pp = GraphicsDevice.PresentationParameters;
            //Tính toán tỉ lệ màn hình
            float aspectRatio = (float)pp.BackBufferWidth /
                                (float)pp.BackBufferHeight;
            this.Projection = Matrix.CreatePerspectiveFieldOfView(
               FieldOfView, aspectRatio, 0.1f, 1000000.0f);
        }
        public virtual void Update()
        {
        }
        public bool BoundingVolumeIsInView(BoundingSphere sphere)
        {
            return (Frustum.Contains(sphere) != ContainmentType.Disjoint);
        }

        
        protected Vector3 _CameraPosition;

        public Vector3 CameraPosition
        {
            get { return _CameraPosition; }
            set { _CameraPosition = value; }
        }
        protected Vector3 _CameraTarget;

        public Vector3 CameraTarget
        {
            get { return _CameraTarget; }
            set { _CameraTarget = value; }
        }
        protected Vector3 _CameraUpVector;

        public Vector3 CameraUpVector
        {
            get { return _CameraUpVector; }
            set { _CameraUpVector = value; }
        }

        /*
        protected Matrix _View;

        public Matrix View
        {
            get { return _View; }
            set { _View = value; }
        }

        protected Matrix _Projection;

        public Matrix Projection
        {
            get { return _Projection; }
            set { _Projection = value; }
        }
        */
        public Camera()
        {

        }
    }
}
